package com.qimo.controller;

import com.qimo.model.Item;
import com.qimo.model.Order;
import com.qimo.model.User;
import com.qimo.service.ItemService;
import com.qimo.service.OrderService;
import com.qimo.service.ShoppingCartService;
import com.qimo.service.UserService;
import com.qimo.tool.ResultInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.util.List;

@Controller
public class OrderController {
    @Resource
    private UserService userService;
    @Resource
    private ItemService ItemService;
    @Resource
    private OrderService orderService;
    @ResponseBody
    @RequestMapping("/Order/createOrder")
    public ResultInfo createOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入createOrder");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int itemId=Integer.parseInt(request.getParameter("itemId"));
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        int Number=Integer.parseInt(request.getParameter("Number"));
        User tempU=this.userService.selectUser(userName);
        if(!(tempU.getPassWord().equals(passWord)&&tempU.getUserId()==userId))
            return ResultInfo.failure("用户信息有误");
        Item temp=this.ItemService.ItemItem(itemId);
        if(Number>temp.getItemNumber()) {
//客户订单数比库存大 无法购买
            return ResultInfo.failure("库存不够");
        }
        else {
            Order order=new Order();
            order.setItemId(itemId);
            order.setNumber(Number);
            order.setUserId(userId);
            order.setPrice(temp.getItemPrice()*Number);
            if(this.orderService.createOrder(order)>=1)return ResultInfo.success();
            else return ResultInfo.failure("创建订单失败");
        }
    }
    @ResponseBody
    @RequestMapping("/Order/selectOrderList")
    public ResultInfo selectOrderList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入selectOrder");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int userId=Integer.parseInt(request.getParameter("userId"));
        int Page=Integer.parseInt(request.getParameter("Page"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        User temp=this.userService.selectUser(userName);
        if(!(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId))
            return ResultInfo.failure("用户信息有误");
        ResultInfo resultInfo=new ResultInfo();
        resultInfo.setResult(this.orderService.selectOrderList(userId,null,Page));
        resultInfo.setCode(1);
        resultInfo.setCount(this.orderService.selectOrderListCount(userId));
        resultInfo.setMsg("查询订单成功");
                return resultInfo;

        }

    @ResponseBody
    @RequestMapping("/HistoryOrder/selectHistoryOrderList")
    public ResultInfo selectHistoryOrderList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入selectHistoryOrder");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        int Page=Integer.parseInt(request.getParameter("Page"));
        User temp=this.userService.selectUser(userName);
        if(!(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId))//验证是否是本人
            return ResultInfo.failure("用户信息有误");
        ResultInfo resultInfo=new ResultInfo();
        System.out.println("1223");
        resultInfo.setResult(this.orderService.selectHistoryOrderList(userId,Page));
        resultInfo.setCode(1);
        System.out.println("344");
        resultInfo.setCount(this.orderService.selectHistoryOrderListCount(userId));
        resultInfo.setMsg("查询订单成功");
        return resultInfo;

    }

    @ResponseBody
    @RequestMapping("/HistoryOrder/delHistoryOrder")
    public ResultInfo delHistoryOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入delHistoryOrder");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int historyOrderId=Integer.parseInt(request.getParameter("historyOrderId"));
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        User temp=this.userService.selectUser(userName);
        if(!(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId))
            return ResultInfo.failure("用户信息有误");
        System.out.println(historyOrderId);
        System.out.println(userId);
        if(this.orderService.delHistoryOrder(historyOrderId,userId)>0)
            return ResultInfo.success();
        else return ResultInfo.failure("完成失败");
    }

    @ResponseBody
    @RequestMapping("/Order/cancelOrder")
    public ResultInfo cancelOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入cancelOrder");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int orderId=Integer.parseInt(request.getParameter("orderId"));
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        User temp=this.userService.selectUser(userName);
        if(!(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId))
            return ResultInfo.failure("用户信息有误");
        if(this.orderService.cancelOrder(orderId,userId))
            return ResultInfo.success();
        else return ResultInfo.failure("取消订单失败");


    }
    @ResponseBody
    @RequestMapping("/Order/completeOrder")
    public ResultInfo completeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入completeOrder");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int orderId=Integer.parseInt(request.getParameter("orderId"));
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        User temp=this.userService.selectUser(userName);
        if(!(temp.getPassWord().equals(passWord)&&temp.getUserId()==userId))
            return ResultInfo.failure("用户信息有误");
        if(this.orderService.completeOrder(orderId,userId))
            return ResultInfo.success();
        else return ResultInfo.failure("完成失败");
    }


}
